package com.android.gilastv.adapter;

import android.content.Context;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.android.baselibrary.interface_.CommCallBack;
import com.android.baselibrary.interface_.OkHttpCallBack;
import com.android.baselibrary.responce.BaseResponce;
import com.android.baselibrary.tool.CommLoading;
import com.android.baselibrary.tool.CommToast;
import com.android.baselibrary.tool.Log;
import com.android.gilastv.R;
import com.android.gilastv.bean.CommVideoInfo;
import com.android.gilastv.constants.Constants;
import com.android.gilastv.manager.API_Manager;
import com.android.gilastv.manager.UserManager;
import com.android.gilastv.ui.view.ExoMedia;

import org.song.videoplayer.DemoQSVideoView;
import org.song.videoplayer.media.IjkMedia;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/8/22.
 */

public class HomeVideoListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    Context mContext;
    List<CommVideoInfo> list;
    CommCallBack callBack;
    List<ContentViewHolder> viewHolderList = new ArrayList<>(); // 保存所有的 ViewHolder

    public HomeVideoListAdapter(Context mContext, CommCallBack callBack) {
        this.mContext = mContext;
        this.callBack = callBack;
    }

    public void setData(List<CommVideoInfo> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater mInflater = LayoutInflater.from(mContext);
        ViewGroup viewGroup = (ViewGroup) mInflater.inflate(R.layout.item_home_videolist, parent, false);
        ContentViewHolder holder = new ContentViewHolder(viewGroup);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ContentViewHolder viewHolder = (ContentViewHolder) holder;
        final CommVideoInfo info = list.get(position);
        //数据绑定逻辑
        viewHolder.tv_video_title.setText(info.hanyuName);
        if (info.videoSetsList != null && info.videoSetsList.size() > 1) {
            viewHolder.tv_xuanji.setVisibility(View.VISIBLE);
        } else {
            viewHolder.tv_xuanji.setVisibility(View.GONE);
        }

        try {
            String url = info.getVideoUrl(info.videoSetsList.get(info.currentSelectVideoIndex).fileId);
            Log.i("url:" + url);
            viewHolder.qs_videoview.setUp(url, info.name);
        } catch (Exception e) {

        }


//        viewHolder.qs_videoview.getCoverImageView().setImageResource(info.getCoverUrl());

        bindInfo(viewHolder, position);

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                if (callBack != null) {
//                    callBack.onResult(info);
//                }
            }
        });
        viewHolder.tv_qingxidu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (callBack != null) {
                    Pair<String, CommVideoInfo> infoPair = new Pair<>("qingxidu", info);
                    callBack.onResult(infoPair);
                }
            }
        });
        viewHolder.tv_xuanji.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (callBack != null) {
                    Pair<String, CommVideoInfo> infoPair = new Pair<>("xuanji", info);
                    callBack.onResult(infoPair);
                }
            }
        });

        viewHolderList.add(viewHolder);
    }

    public void setViewVisible(boolean isVisible) {
//        Log.i("viewHolderList:" + viewHolderList.size());
        for (ContentViewHolder viewHolder : viewHolderList) {
            if (isVisible) {
                viewHolder.rl_btn.setVisibility(View.VISIBLE);
            } else {
                viewHolder.rl_btn.setVisibility(View.GONE);
            }
        }
    }

    private void bindInfo(ContentViewHolder viewHolder, int position) {
        CommVideoInfo info = list.get(position);
        if (info.likeFlag) {
            viewHolder.iv_dianzan.setImageResource(R.drawable.ico_dianzan_red);
        } else {
            viewHolder.iv_dianzan.setImageResource(R.drawable.ico_dianzan_white);
        }
        if (info.collectFlag) {
            viewHolder.iv_shoucang.setImageResource(R.drawable.ico_shoucang_red);
        } else {
            viewHolder.iv_shoucang.setImageResource(R.drawable.ico_shoucang_white);
        }

        //点赞
        viewHolder.iv_dianzan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ////add，del
                if (UserManager.isLoginAndShowLogin(mContext)) {
                    if (info.likeFlag) {
                        userLike(viewHolder, position, "del");
                    } else {
                        userLike(viewHolder, position, "add");
                    }
                }
            }
        });
        //收藏
        viewHolder.iv_shoucang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (UserManager.isLoginAndShowLogin(mContext)) {
                    if (info.collectFlag) {
                        userCollect(viewHolder, position, "del");
                    } else {
                        userCollect(viewHolder, position, "add");
                    }
                }
            }
        });
//        //分享
//        viewHolder.iv_share.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Map<String, Object> params = new HashMap<>();
//                params.put("info",info);
//                params.put("isLittleVideo",true);
//                Share_DialogFragment.showDialog(((FragmentActivity)mContext).getSupportFragmentManager(),params);
//            }
//        });

    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }


    public class ContentViewHolder extends RecyclerView.ViewHolder {
        TextView tv_video_title, tv_qingxidu, tv_xuanji;
        public RelativeLayout rl_container, rl_btn;
        ImageView iv_dianzan, iv_shoucang;
        public DemoQSVideoView qs_videoview;

        public ContentViewHolder(View itemView) {
            super(itemView);
            tv_video_title = itemView.findViewById(R.id.tv_video_title);
            rl_container = itemView.findViewById(R.id.rl_container);
            tv_qingxidu = itemView.findViewById(R.id.tv_qingxidu);
            tv_xuanji = itemView.findViewById(R.id.tv_xuanji);
            iv_dianzan = itemView.findViewById(R.id.iv_dianzan);
            iv_shoucang = itemView.findViewById(R.id.iv_shoucang);
            rl_btn = itemView.findViewById(R.id.rl_btn);

            qs_videoview = itemView.findViewById(R.id.qs_videoview);
            qs_videoview.setDecodeMedia(IjkMedia.class);
            qs_videoview.fullscreenButton.setVisibility(View.GONE);

            itemView.setTag(this);
        }
    }


    //收藏
    private void userCollect(ContentViewHolder viewHolder, int position, String type) {
        CommVideoInfo info = list.get(position);
        API_Manager.userCollect(mContext, type, info.id, new OkHttpCallBack<BaseResponce<Object>>() {
            @Override
            public void onSuccess(BaseResponce<Object> baseResponce) {
                if ("del".equals(type)) {
                    CommToast.showToast(mContext, "已取消");
                } else {
                    CommToast.showToast(mContext, "已收藏");
                }

                getDetail(viewHolder, position);
            }

            @Override
            public void onFailure(BaseResponce<Object> baseResponce) {
                CommLoading.dismissLoading();
                CommToast.showToast(mContext, Constants.NetError);
            }
        });
    }

    //点赞
    private void userLike(ContentViewHolder viewHolder, int position, String type) {
        CommVideoInfo info = list.get(position);
        API_Manager.userLike(mContext, type, info.id, new OkHttpCallBack<BaseResponce<Object>>() {
            @Override
            public void onSuccess(BaseResponce<Object> baseResponce) {
                if ("del".equals(type)) {
                    CommToast.showToast(mContext, "已取消");
                } else {
                    CommToast.showToast(mContext, "已点赞");
                }
                getDetail(viewHolder, position);
            }

            @Override
            public void onFailure(BaseResponce<Object> baseResponce) {
                CommLoading.dismissLoading();
                CommToast.showToast(mContext, Constants.NetError);
            }
        });
    }

    //获取详情
    private void getDetail(ContentViewHolder viewHolder, int position) {
        CommVideoInfo info = list.get(position);
        API_Manager.getVideoInfo(mContext, info.id, new OkHttpCallBack<BaseResponce<CommVideoInfo>>() {
            @Override
            public void onSuccess(BaseResponce<CommVideoInfo> baseResponce) {
                list.set(position, baseResponce.getData());
                bindInfo(viewHolder, position);
            }

            @Override
            public void onFailure(BaseResponce<CommVideoInfo> baseResponce) {
                CommLoading.dismissLoading();
                CommToast.showToast(mContext, Constants.NetError);
            }
        });
    }
}
